﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Google.GData.Client;
using Google.GData.Documents;

namespace iPdf
{
    class GoogleDocsPublisher: CloudStorageService
    {
        #region Fields
        //The google docs service object
        protected DocumentsService service;

        //feed uri
        private const string DocListFeedUri = "http://docs.google.com/feeds/default/private/full";

        public static readonly GoogleDocsPublisher Instance = new GoogleDocsPublisher();
        #endregion

        #region Properties
        public override List<string> Folders
        {
            get
            {
                return new List<string>();
            }
        }

        #endregion 

        #region Ctor
        private GoogleDocsPublisher()
        {
            serviceName = "Google Docs";
            supportedFileFormats.Description = "Google Docs Supported Formats";
            foreach(DictionaryEntry entry in DocumentsService.GDocumentsAllowedTypes)
            {
                supportedFileFormats.AddExtension((string)entry.Key);
            }
        }
        #endregion

        /// <summary>
        /// log in to the service
        /// </summary>
        /// <param name="username">The username</param>
        /// <param name="password">The password</param>
        /// <returns></returns>
        public override void Login(string username, string password)
        {
            try
            {
                service = new DocumentsService(Global.ApplicationName);
                GDataGAuthRequestFactory reqFactory = (GDataGAuthRequestFactory)service.RequestFactory;
                reqFactory.KeepAlive = false;
                reqFactory.ProtocolMajor = 3;
                reqFactory.Timeout = Int32.MaxValue;
                service.setUserCredentials(username, password);

                loggedIn = true;
            }
            catch
            {
                loggedIn = false;
                service = null;
                throw new ServiceLoginFailed(serviceName);
            }

        }

        /// <summary>
        /// Upload the specified file to the google docs server
        /// </summary>
        /// <param name="fileName">the file to be uploaded</param>
        /// <returns></returns>
        public override void Upload(string fileName)
        {
            base.Upload(fileName);

            FileInfo fileInfo = new FileInfo(fileName);
            FileStream stream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            try
            {
                string ext = fileInfo.Extension.Substring(1).ToUpper();
                string contentType = (string)DocumentsService.GDocumentsAllowedTypes[ext];
                service.Insert(new Uri(DocListFeedUri), stream, contentType, 
                    Path.GetFileNameWithoutExtension(fileName));
            }
            finally
            {
                stream.Close();
            }
        }
    }
}
